More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

The Skyscraper Smart Lift Algorithm

Imagine you are the lead developer for a cutting-edge, luxurious hotel housed in a 101-floor skyscraper. The hotel’s main lift needs a smart algorithm to efficiently transport guests to their rooms. Each floor has seven rooms, and the numbering starts from the ground floor (Floor 0) with rooms 1 to 7, then Floor 1 with rooms 8 to 14, and so on, up to Floor 100 with rooms 701 to 707.

The smart lift of this hotel is equipped with a keypad that let the guests enter their room number.

Your challenge is to write a Python algorithm to control this smart lift. Your algorithm will need to:

    Take a room number as input
    Validate this room number to only accept room numbers between 1 and 707 and display an error message if the room number is invalid
    Calculate and output the corresponding floor number that the lift will have to reach.

Key Insight

You may decide to solve this problem by yourself. Alternatively you can read through the following guidance to help you solve this challenge.

Let’s first make sense of the room numbering pattern:

  • Ground Floor (Floor 0): Rooms 1, 2, 3, 4, 5, 6, 7
  • Floor 1: Rooms 8, 9, 10, 11, 12, 13, 14
  • Floor 2: Rooms 15, 16, 17, 18, 19, 20, 21
  • Floor 100: Rooms 701, 702, 703, 704, 705, 706, 707

The floor number for any given room can be calculated using integer division (//). For example:

Room 24: 24 // 7 = 3. This room will be located on the 3rd floor.

However we need to be cautious, as just applying an integer division will not work for every room:
For instance: Room 7: 7 // 7 = 1 though we know that room 7 should be on the ground floor (0).

We can therefore define a formula to help us get the correct floor number for every valid room number:

Python Code

Your turn, use the online Python IDE below to complete the code for this challenge. Then you will be able to test your code with the test plan provided below the Python IDE.

Test Plan

Use the following test plan to test your code:

Input Expected Output Pass/Fail?
Room Number: 5 Ground Floor
Room Number: 12 1
Room Number: 16 2
Room Number: 21 2
Room Number: 22 3
Room Number: 76 10
Room Number: 101 14
Room Number: 707 100
Room Number: 708 Invalid Room Number!
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Kaprekar’s Constant – Python Challenge

In this challenge, we’re going to explore a fascinating mathematical curiosity known as Kaprekar’s Constant, and then write a Python program to demonstrate how it works.

Kaprekar’s constant, number 6174 has an odd particularity: If you rearrange its four digits in descending order (7641) and take away the number made of these same 4 digits in ascending order (1467) you will fall back on the initial number 7641 – 1467 = 6174!

But that’s not all… The Indian mathematician D. R. Kaprekar discovered something amazing about 4-digit numbers. Here’s the process he found:

  1. Take any 4-digit number (at least two different digits).
    Example: 3524

  2. Rearrange its digits to create:
    • The largest possible number: 5432
    • The smallest possible number: 2345
  3. Subtract the smaller number from the larger one:
    5432 – 2345 = 3087

  4. Now take this result and repeat the process!
    Step 1: 5432 – 2345 = 3087
    Step 2: 8730 – 0378 = 8352
    Step 3: 8532 – 2358 = 6174
    Step 4: 7641 – 1467 = 6174

Once you reach 6174, the process stops — because you’ll keep getting 6174 forever!

This number, 6174, is known as Kaprekar’s Constant. No matter which 4-digit number you start with (as long as the digits aren’t all identical), you will always end up at 6174 after a few iterations!

Let’s visualise this with 2025 as our starting 4-digit number:

Python Challenge

The initial challenge is to write a Python program that:

  • Asks the user to enter a 4-digit number (with at least two different digits).
  • Applies Kaprekar’s routine (rearrange digits, subtract, repeat).
  • Displays each step of the process.
  • Stops once it reaches 6174.
  • Outputs how many iterations it took to reach the constant.

Example output:

Enter a 4-digit number: 3524

Step 1: 5432 - 2345 = 3087
Step 2: 8730 - 0378 = 8352
Step 3: 8532 - 2358 = 6174

It took 3 iterations to reach Kaprekar's constant (6174).

Python Code (Solution)

Extra Challenge

Write a program to count how many iterations it takes for every 4-digit number (from 0001 to 9998) to reach 6174. Use this program to find out both the average (mean) and the maximum number of iterations it takes to reach Kaprekar’s constant amongst all these numbers!

Warning: Make sure to remove any number with the same 4 digits (1111, 2222, 3333… as these cannot lead to Kaprekar’s constant: When applying the process described above on such a 4-digit number, the computer will loop indefinitely without reaching Kaprekar’s constant.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

The Recamán’s Sequence (Recursive Algorithm)

The Recamán’s Sequence is a well-known recursive sequence used in Mathematics and Computer Science. It can easily be implemented using a recursive function.

Recamán’s sequence a0, a1, a2, … is defined as:

The first terms of the sequence are:
0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8, 25, 43, 62, 42, 63, 41, 18, 42, 17, 43, 16, 44, 15, 45, 14, 46, 79, 113, 78, 114, 77, 39, 78, 38, 79, 37, 80, 36, 81, 35, 82, 34, 83, 33, 84, 32, 85, 31, 86, 30, 87, 29, 88, 28, 89, 27, 90, 26, 91, 157, 224, 156, 225, 155, …

By drawing an arc between each consecutive number of the Recamán’s Sequence we can create beautiful visual representations of the sequence:

The above visualisation represents the first 70 numbers of the Recamán’s Sequence

To create this visual representation, we start with a0=0 and then draw an arc between each consecutive set of values of the sequence:

Recursive Function

Let’s implement a recursive function in Python to generate the first n values of the Recamán’s sequence:

Tagged with:

Flood-Fill Algorithm

The aim of this challenge is to demonstrate the use of a recursive algorithm used in most Graphic Editing Software when you use the fill tool to quickly and automatically fill a selected area with a solid colour.

To demonstrate this algorithm we will first initialise a 2D-array called grid that will represent our picture, each cell being a pixel of colour. We will use three integer values to represent the colours white (=0), black (=1) and purple (=2).

We will prefill our grid with mainly white pixels as well as a few random black blocks randomly positioned on the grid. We will then use a recursive algorithm, starting from a random position on the grid, to colour all adjacent white pixels using a recursive approach:

Python Code

Here is the Python code to create a visual demonstration of our recursive flood-fill algorithm:

Tagged with:

Random Alphabet Challenge

Welcome to the Random Alphabet Challenge! This set of three Python programming challenges is designed to test your problem-solving skills, creativity, and ability to manipulate strings and data structures.

But first let’s see if you can solve the following puzzle:

Coding Challenge #1: Find the Correctly Placed Letter.

You are given a string of 26 uppercase letters, representing a shuffled version of the English alphabet. In this shuffled string, only one letter remains in its correct position (i.e., its position in the alphabet). Your task is to write a Python script that identifies which letter is correctly placed.

Example:

  • Input: “IVKOCEPJLTAMZNSYXUWRHFBDGQ”
  • Output: “N” (because “N” is the 14th letter of the alphabet and also the 14th letter in the given string)

Python Code
Complete your code using the following online Python IDE:

Suggested Approach
Our recommended approach to solve this challenge is write a Python program to:

  • Iterate through each letter in the input string.
  • Compare the position of each letter in the string with its position in the alphabet.
  • Identify the letter where these positions match.

Testing
See if your code can find the letter in the correct position for the following shuffled alphabets:

Test # Input Values Output?
#1 SZDABJWLNRITXOUGQHEFPMVCKY
#2 OLMETJKXNGVYRZIWADSHPCQFBU
#3 XMBYCNGRQOVZPFWTJLHKEIUADS

Coding Challenge #2: Alphabet Shuffling Algorithm

In Stage 1, you wrote a Python script to identify the correctly placed letter in a shuffled alphabet string. Now, in Stage 2, we flip the script: your task is to generate a shuffled alphabet string where only one letter remains in its correct position.

You will need to write a Python program that:

  • Generates a random permutation of the 26 uppercase letters of the English alphabet.
  • Ensures that only one letter in this permutation is in its correct alphabetical position (e.g., “A” at index 0, “B” at index 1, etc.).

Coding Challenge #3: Quiz Time

This is the final stage of the Random Alphabet Challenge! In Stage 1, you identified the correctly placed letter in a shuffled alphabet. In Stage 2, you generated such a shuffled string. Now, in Stage 3, we combine both to create an interactive 10-round quiz where the player must identify the correctly placed letter in each round. Each correct answer scores one point.

Your Python script will need to:

  • Generate 10 shuffled alphabet strings (using your Stage 2 code), each with only one correctly placed letter.
  • Present each shuffled string to the user and asks them to identify the correctly placed letter.
  • Award one point for each correct answer (you may need to use your code from stage 1 for the computer to identify the correct letter and use this to check if the player’s answer is correct)
  • Display the final score at the end.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Storage Technologies – Tic-Tac-Toe (Python Challenge)

For this challenge, we are putting a tech twist on the classic Tic-Tac-Toe game. Instead of Xs and Os, you will be working with a 3×3 grid filled with various computer storage devices. Your task is to write a Python program that not only generates this grid but also analyses it to detect if three devices of the same technology are aligned horizontally, vertically, or diagonally. Your program will give a score to the randomly generated grid where each alignment will add 10 points to the grid score.

Learning Objectives

The aim of this challenge is to work with different data structures in Python. The code provided below will use:

  • A 2D-array to store the tic-tac-toe grid (3×3 grid)
  • A list of storage devices
  • A dictionary of storage devices to store the technologies of each storage device

Your Challenge

Step 1:

Generate a 3×3 grid filled with random storage devices from the following list:

  • CD
  • DVD
  • BluRay
  • HDD
  • Floppy Disk
  • Magnetic Tape
  • USB Key
  • SD Card
  • SSD

Step 2:

Analyse the grid to check for any horizontal, vertical, or diagonal alignments of three devices from the same technology category:

  • Optical: CD, DVD, BluRay
  • Magnetic: HDD, Floppy Disk, Magnetic Tape
  • Solid State: USB Key, SD Card, SSD

Step 3:

Score points: For every alignment of three devices from the same technology, increase the score by 10.

Python Code

The code is already started for you. The code generates the random 3×3 grid of storage devices (step 1). But step 2 and 3 are incomplete. A this stage the code only checks the first row of the grid. You will need to complete this code t check all three rows as well as all thre columns and the two diagonales.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Lesson Finder (Python Challenge)

In this project, you’ll design a Python program that stores your school timetable (e.g. 5 lessons per day, Monday to Friday) and allows you to quickly find out what lesson you have on any given day and period. Simply enter a day (e.g. Tuesday) and a lesson number (e.g. 2), and the program will instantly tell you what subject you have, who the teacher is, and where the class is held.

Example Output:

“On Tuesday, Lesson 2 you have a Maths lesson with Mr. Smith in Room 105.”

Challenge Objectives

By completing this project, you will gain experience with:

  • Data Structures: Using dictionaries and lists to store and organise your timetable.
  • Input Validation: Taking input from the user and applying validation checks on the data being entered.
  • Output Formatting: Creating a user-friendly messages for the user to provide them with the required information.

Step 1: Storing the timetable using dictionaries and lists in Python

To complete this challenge, our first step will be to store our timetable within the code of our program. We will do so by using a combination of dictionaries and lists in Python.

  • Dictionaries ({}) are used to store data as key-value pairs. For example, in the Lesson Finder, each day of the week (e.g., “Monday”) is a key, and its value is a list of lessons for that day.
  • Lists ([]) are used to store ordered collections of items. In this project, each day’s lessons are stored as a list of dictionaries, where each dictionary contains details like the subject, teacher, and room.

To store the 5-day timetable so we will use a dictionary: Each key will represent a day, and the value will be a list of lessons for that day:

timetable = {
    "Monday": [
        {"subject": "English", "teacher": "TS", "room": "201"},
        {"subject": "Maths", "teacher": "MX", "room": "105"},
        {"subject": "Biology", "teacher": "SC", "room": "302"},
        {"subject": "History", "teacher": "HT", "room": "110"},
        {"subject": "Art", "teacher": "AR", "room": "403"}
    ],
    "Tuesday": [
        {"subject": "Maths", "teacher": "MX", "room": "105"},
        {"subject": "Physics", "teacher": "PH", "room": "301"},
        {"subject": "English", "teacher": "TS", "room": "201"},
        {"subject": "PE", "teacher": "PB", "room": "Gym"},
        {"subject": "ICT", "teacher": "KN", "room": "205"}
    ],
    # Add the rest of your days here
}

JSON Syntax?

If you are familiar with JSON syntax, you may have noticed that the syntax of our timetable built using a mix of dictionaries and lists in Python mirrors the JSON syntax (JavaScript Object Notation). JSON is a lightweight data format that is easy for humans to read and write, and easy for machines to parse and generate. It’s widely used for storing and exchanging data, especially in web applications. In Python, using a combination of dictionaries and lists allows us to create a structured, nested format that is both flexible and easy to work with—just like JSON!

Step 2: Receiving and validating user inputs

Now that we have stored our timetable in our Python code, we can retrieve the two inputs from the user: The day of the Week (Monday to Friday) and the period (1 to 5) of the lesson.

For our first input, we will use a look-Up check to validate the day of the week as we will only accept the following values: Monday, Tuesday, Wednesday, Thursday, Friday.

day = input("Enter the day of the week: ").title()
while day not in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]:
   print("Invalid input! Please try again.")
   day = input("Enter the day of the week: ").title()

For our second input, we will use a range check to make sure only a value between 1 and 5 is being entered.

lessonNumber = int(input("Enter the lesson number (1-5): "))
while lessonNumber <1 or lessonNumber >5:
   print("Invalid input! Please try again.")
   lessonNumber = int(input("Enter the lesson number (1-5): "))

Step 3: Extracting the required information from the timetable

The following code enables us to access the different values from our timetable:

lesson = timetable[day][lessonNumber - 1]
subject = lesson["subject"]
teacher = lesson["teacher"]
room = lesson["room"]

We can then output a user-friendly message to the end-user using string concatenation:

print("On " + day + ", lesson " + str(lessonNumber) + ", you have a " + subject + " lesson with " + teacher + " in room " + room + ".")

Python Code

Here is the Python code for this challenge. You can tweak this code to enter your own timetable:

Your Challenge

Can you tweak this code to start by generating a printout of the whole timetable. The output would look like this:

>>> Weekly Timetable <<<

>>> Monday
> Lesson 1: English with TS in room 201.
> Lesson 2: Maths with MX in room 105.
> Lesson 3: Biology with SC in room 302.
> Lesson 4: History with HT in room 110.
> Lesson 5: Art with AR in room 403.

>>> Tuesday
> Lesson 1: Maths with MX in room 105.
> Lesson 2: Physics with PH in room 301.
> Lesson 3: English with TS in room 201.
> Lesson 4: PE with PB in room Gym.
> Lesson 5: ICT with KN in room 205.
...
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Programming Languages Classification (Python Challenge)

Choosing the right programming language can feel overwhelming, especially for beginners. Should you learn Python, JavaScript, Swift, or something else? The answer depends on what you want to build! That’s why we are launching a fun and practical Python challenge: Build a Programming Language Recommender.

In this challenge, you’ll create a Python program that guides users to the best programming language for their project. Whether they want to build a website, a mobile app, a video game, or even create a computer program to control an embedded device, your program will ask the right questions and provide tailored recommendations.

How It Works

Your task is to write a Python program that interacts with the user through a series of questions. Based on their answers, your program will suggest one or more programming languages that fit their goals.

Your algorithm will be based on the following classification (Click on the picture to open in a new window).

The first question your program will need to ask is:

What would you like to create?

The user will have to pick an option: Website, Mobile App, Video Game, Desktop Software or Embedded System

Based on the user choice, the program will then ask follow-up questions. For instance, if the user selects “Mobile App” the program will then ask:

Is this app for an iPhone or for an Android phone?

Based on the answers provided by the end-user, the program will suggest the most relevant programming languages the end-user should consider for their project.

Decision Tree Logic

To complete this challenge you will need to use decision tree logic to implement the given classification (see picture above). To to do so you will have to use several nested IF, ELIF and ELSE statements within your code.

Here is an example of what your code may look like:

print("Welcome to the Programming Language Recommender!")
project = input("What would you like to create? (Website/Mobile App/Video Game/Desktop Software/Embedded System): ").lower()

if project == "mobile app":
   platform = input("Is this app for iPhone or an Android phone? ").lower()
   if platform == "iphone":
      print("We recommend learning Swift or Dart!")
   elif platform == "android":
      print("We recommend learning Kotlin or JavaScript, Python or Dart!")
   else:
      print("Invalid Option!")

# Add more conditions for other types of projects!

When using nested if statements within an algorithm, make sure to indent your code properly:

Python Code

Use our online Python IDE to complete this challenge:

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Uno Bank – Card Game (using Python)


Are you ready to test your luck and strategy with a Python twist on the classic Uno card game? In Uno Bank, you’ll start with $10 in your virtual bank and 10 card draws. Your goal? Maximize your bank balance by drawing cards that either add to or subtract from your total. But beware—some cards can double your money, halve it, or even change the number of draws you have left!

This challenge is perfect for Python beginners and enthusiasts who want to practice their programming skills while having fun. Let’s dive into the rules, the logic, and how you can build this game yourself.

Game Rules

Game Objective
Start with $10 and 10 draws. Draw cards to increase your bank balance. The game ends when you run out of draws, and your final balance is displayed.

Card Types and Effects

Green Cards: Increase your bank balance by the amount on the card (e.g., +3$).

Blue Cards: Decrease your bank balance by the amount on the card (e.g., -4$).

Black Cards:

  • x2: Double your current bank balance.
  • /2: Halve your current bank balance.
  • +2: Gain 2 extra draws.
  • -2: Lose 2 draws.

How to Build the Game in Python

Step 1: Set Up the Deck
Create a deck of cards with the following distribution:

    Green Cards: 4 cards (+1$, +3$, +5$, +7$)
    Blue Cards: 4 cards (-2$, -4$, -6$, -8$)
    Black Cards: 4 cards (x2, /2, +2, -2)

You can use a list to represent the deck and shuffle it at the start of the game.

Step 2: Initialise the Game

    Start with a bank balance of $10.
    Start with 10 draws.

Step 3: Game Loop

    Draw a Card: Randomly select a card from the deck.
    Apply the Effect:

      If it’s a green card, add the amount to the bank.
      If it’s a blue card, subtract the amount from the bank.
      If it’s a black card, apply the special effect (double, halve, or change the number of draws).

    Update Draws: Decrease the number of draws by 1 (unless a +2 or -2 card is drawn).
    Repeat until no draws are left.

Step 4: Display the Result
Once all draws are used, display the final bank balance.

Python Code

Complete the code for this Uno-Bank game using our online Python IDE:

Extension Task: 2-Player Mode!

Ready to take Uno Bank to the next level? In this two-player extension, you and a friend will compete to maximise your bank balances by drawing cards from a shared deck. The rules remain similar, but now, you will take turns drawing cards, and a new set of yellow cards introduces exciting (and sometimes brutal) interactions between players!

New Rules for Two-Player Mode

Objective
Both players start with $10 and 10 draws. Players take turns drawing one card at a time. The game ends when one of the players have used all their draws. The player with the highest bank balance wins!

New Cards
The game uses the same cards as before plus an extra 4 yellow cards:

  • Swap Card: Both players swap their bank balances.
  • Forward Card: Give all your money to the other player.
  • Backward Card: Take all the money from the other player.
  • Bomb Card: Your bank balance resets to $0.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Computer Hardware Quote Generator

Have you ever wondered how computer manufacturers calculate the price of a custom PC? Or maybe you have dreamed of building your own computer but were not sure how much it would cost? In this Python challenge, you’ll step into the shoes of a computer builder! Your mission: write a program that lets users pick their ideal hardware components and generates a quote price for their custom computer.

Your Challenge

For this challenge you will create a Python program that:

  • Displays a menu of hardware options (e.g., screen size, RAM, CPU, storage).
  • Lets the user select their preferred components.
  • Calculates the total price based on their choices.
  • Outputs a detailed quote with the selected specifications and total cost.

Hardware Options

Your program should allow users to customise at least the following components:

Component Options (with Base Prices)
Screen Size 13″ (£50), 15″ (£100), 17″ (£150)
RAM 8GB (£40), 16GB (£80), 32GB (£150)
CPU Clock Speed 2.5GHz (£100), 3.2GHz (£200), 4.0GHz (£350)
Storage 256GB HDD (£30), 512GB SSD (£60), 1TB SSD (£120)
Graphics Card Integrated (£0), Dedicated (£150), High-End (£400)

Feel free to add more components or options in your code!

Sample Output

Welcome to the Custom Computer Quote Generator!

Please select your screen size:
a. 13" (£50)
b. 15" (£100)
c. 17" (£150)
> b

Please select your RAM:
a. 8GB (£40)
b. 16GB (£80)
c. 32GB (£150)
> b

Please select your CPU clock speed:
a. 2.5GHz (£100)
b. 3.2GHz (£200)
c. 4.0GHz (£350)
> c

Please select your storage:
a. 256GB HDD (£30)
b. 512GB SSD (£60)
c. 1TB SSD (£120)
> c

---
Your Custom Computer Quote:
 - Screen Size: 15" (£100)
 - RAM: 16GB (£80)
 - CPU: 4.0GHz (£350)
 - Storage: 1TB SSD (£120)
TOTAL PRICE: £650
---

Python Code

We have started the code for you but you will need to complete it to:

    Let the user pick up more hardware options
    Calculate and output the total cost of the PC
    Display a summary of the selected hardware specifications

Extension Task: Entering a Discount Code

Once the user has made their selection, they should be prompted to enter a voucher code. The system currently accepts the following three voucher codes:

  • PROMO-10 which gives a 10% discount on the total cost of the computer.
  • PROMO-20 which gives a 20% discount on the total cost of the computer.
  • PROMO-30 which gives a 30% discount on the total cost of the computer but is only applicable on computers costing £600 or more.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area